package cn.codeinn.service1.service.impl;

import cn.codeinn.service1.dto.MemberDTO;
import cn.codeinn.service1.entity.Member;
import cn.codeinn.service1.myinterface.MyFunctionInterface;
import cn.codeinn.service1.service.LambdaService;
import org.junit.Test;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service("lambdaService")
public class LambdaServiceImpl implements LambdaService {

    private List<MemberDTO> members = new ArrayList<>();

    @PostConstruct
    public void initMembers() {
        MemberDTO memberDTO0 = new MemberDTO();
        memberDTO0.setId(1);
        memberDTO0.setName("A");
        memberDTO0.setLevel(100);
        memberDTO0.setTargetNum(20);

        MemberDTO memberDTO1 = new MemberDTO();
        memberDTO1.setId(1);
        memberDTO1.setName("A");
        memberDTO1.setLevel(200);
        memberDTO1.setTargetNum(30);

        MemberDTO memberDTO2 = new MemberDTO();
        memberDTO2.setId(1);
        memberDTO2.setName("A");
        memberDTO2.setLevel(300);
        memberDTO2.setTargetNum(10);

        MemberDTO memberDTO3 = new MemberDTO();
        memberDTO3.setId(1);
        memberDTO3.setName("A");
        memberDTO3.setLevel(150);
        memberDTO3.setTargetNum(60);

        members.add(memberDTO0);
        members.add(memberDTO1);
        members.add(memberDTO2);
        members.add(memberDTO3);

    }

    @Override
    public void test1() {
        //1
        Collections.sort(members);

        Collections.sort(members, new Comparator<MemberDTO>() {
            @Override
            public int compare(MemberDTO o1, MemberDTO o2) {
                return o2.getLevel().compareTo(o1.getLevel());
            }
        });

        for (MemberDTO memberDTO : members) {
            System.out.println(memberDTO.toString());
        }

        Collections.sort(members, new Comparator<MemberDTO>() {
            @Override
            public int compare(MemberDTO o1, MemberDTO o2) {
                return o2.getTargetNum().compareTo(o1.getTargetNum());
            }
        });

        for (MemberDTO memberDTO : members) {
            System.out.println(memberDTO.toString());
        }

        members.sort((o1, o2) -> o2.getTargetNum().compareTo(o1.getTargetNum()));

        Comparator<MemberDTO> comparator = new Comparator<MemberDTO>() {
            @Override
            public int compare(MemberDTO o1, MemberDTO o2) {
                return o2.getLevel().compareTo(o1.getLevel());
            }
        };
        members.sort(comparator);
        for (MemberDTO memberDTO : members) {
            System.out.println(memberDTO.toString());
        }


    }

    @Test
    public void test12() {
        myFunction(new MyFunctionInterface() {
            @Override
            public void test(Integer a) {
                System.out.println(a);
            }
        });

        myFunction((x) -> System.out.println(x));

        myFunction(System.out::println);
    }

    private void myFunction(MyFunctionInterface myFunctionInterface) {
        myFunctionInterface.test(1);
    }

    public void test23() {
        int count = 0;
        for (MemberDTO memberDTO : members) {
            if (memberDTO.getLevel() > 200) {
                if (memberDTO.getTargetNum() > 50) {
                    count++;
                }
            }
        }
        System.out.println(count);

        //
        System.out.println(members.stream().filter(memberDTO -> memberDTO.getLevel() > 200).filter(memberDTO -> memberDTO.getTargetNum() > 60).count());
    }
}
